/*     */ package com.zimbra.cs.util;
/*     */ 
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.ClassLoaderUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import java.io.File;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Arrays;
/*     */ import java.util.Collections;
/*     */ import java.util.List;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class SkinUtil
/*     */ {
/*     */   private static final String LOAD_SKINS_ON_UI_NODE = "/fromservice/loadskins";
/*     */   private static final String FLUSH_SKINS_ON_UI_NODE = "/fromservice/flushskins";
/*  38 */   private static String[] sSkins = null;
/*     */   
/*     */   private static synchronized String[] getAllInstalledSkinsSorted() throws ServiceException
/*     */   {
/*  42 */     if (sSkins == null) {
/*  43 */       sSkins = loadSkins();
/*     */     }
/*  45 */     return sSkins;
/*     */   }
/*     */   
/*     */   public static synchronized void flushCache() throws ServiceException {
/*  49 */     sSkins = null;
/*  50 */     if (WebClientServiceUtil.isServerInSplitMode()) {
/*  51 */       WebClientServiceUtil.sendServiceRequestToEveryUiNode("/fromservice/flushskins");
/*     */     }
/*     */   }
/*     */   
/*     */   private static String[] loadSkins() throws ServiceException {
/*  56 */     ZimbraLog.webclient.debug("Loading skins...");
/*  57 */     List<String> skins = new ArrayList();
/*  58 */     if (WebClientServiceUtil.isServerInSplitMode()) {
/*  59 */       String resp = WebClientServiceUtil.sendServiceRequestToOneRandomUiNode("/fromservice/loadskins");
/*  60 */       Collections.addAll(skins, resp.split(","));
/*     */     } else {
/*  62 */       loadSkinsByDiskScan(skins);
/*     */     }
/*  64 */     String[] sortedSkins = (String[])skins.toArray(new String[skins.size()]);
/*  65 */     Arrays.sort(sortedSkins);
/*  66 */     ZimbraLog.webclient.debug("Skin loading complete.");
/*  67 */     return sortedSkins;
/*     */   }
/*     */   
/*     */   public static void loadSkinsByDiskScan(List<String> skins) throws ServiceException {
/*  71 */     String skinsDir = LC.skins_directory.value();
/*  72 */     ClassLoader classLoader = ClassLoaderUtil.getClassLoaderByDirectory(skinsDir);
/*  73 */     if (classLoader == null) {
/*  74 */       throw ServiceException.FAILURE("unable to get class loader for directory " + skinsDir + " configured in localconfig key " + LC.skins_directory.key(), null);
/*     */     }
/*     */     
/*  77 */     File dir = new File(skinsDir);
/*  78 */     File[] files = dir.listFiles();
/*     */     
/*  80 */     if (files != null) {
/*  81 */       for (File f : files) {
/*  82 */         String fname = f.getName();
/*  83 */         if ((!fname.startsWith("_")) && (new File(f, "manifest.xml").exists())) {
/*  84 */           skins.add(fname);
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public static String[] getSkins(Account acct) throws ServiceException {
/*  91 */     String[] installedSkins = getAllInstalledSkinsSorted();
/*  92 */     Set<String> allowedSkins = getAvailableSkins(acct);
/*     */     
/*  94 */     String[] availSkins = null;
/*  95 */     if (allowedSkins.size() == 0) {
/*  96 */       availSkins = installedSkins;
/*     */     } else {
/*  98 */       List<String> skins = new ArrayList();
/*     */       
/* 100 */       for (String skin : installedSkins) {
/* 101 */         if (allowedSkins.contains(skin)) {
/* 102 */           skins.add(skin);
/*     */         }
/*     */       }
/* 105 */       availSkins = (String[])skins.toArray(new String[skins.size()]);
/*     */     }
/* 107 */     return availSkins;
/*     */   }
/*     */   
/*     */   public static String[] getAllSkins() throws ServiceException {
/* 111 */     return getAllInstalledSkinsSorted();
/*     */   }
/*     */   
/*     */   private static Set<String> getAvailableSkins(Account acct) throws ServiceException {
/* 115 */     return acct.getMultiAttrSet("zimbraAvailableSkin");
/*     */   }
/*     */   
/*     */   public static String chooseSkin(Account acct, String requestedSkin) throws ServiceException {
/* 119 */     String[] installedSkins = getAllInstalledSkinsSorted();
/*     */     
/*     */ 
/* 122 */     Set<String> allowedSkins = getAvailableSkins(acct);
/* 123 */     if (checkSkin(requestedSkin, installedSkins, allowedSkins)) {
/* 124 */       ZimbraLog.webclient.debug("Loading requested skin " + requestedSkin);
/* 125 */       return requestedSkin;
/*     */     }
/*     */     
/*     */ 
/* 129 */     String prefSkin = acct.getAttr("zimbraPrefSkin");
/* 130 */     if (checkSkin(prefSkin, installedSkins, allowedSkins)) {
/* 131 */       ZimbraLog.webclient.debug("Loading account skin " + prefSkin);
/* 132 */       return prefSkin;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 137 */     String usuallyAvailableSkin = "harmony";
/* 138 */     if ((prefSkin != usuallyAvailableSkin) && 
/* 139 */       (checkSkin(usuallyAvailableSkin, installedSkins, allowedSkins))) {
/* 140 */       ZimbraLog.webclient.debug("Loading default skin " + usuallyAvailableSkin);
/* 141 */       return usuallyAvailableSkin;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 146 */     if (installedSkins.length > 0) {
/* 147 */       ZimbraLog.webclient.debug("Returning first known skin " + installedSkins[0]);
/* 148 */       return installedSkins[0];
/*     */     }
/*     */     
/*     */ 
/* 152 */     return null;
/*     */   }
/*     */   
/*     */   private static boolean checkSkin(String requestedSkin, String[] installedSkins, Set<String> allowedSkins) {
/* 156 */     if ((requestedSkin != null) && (requestedSkin.length() > 0)) {
/* 157 */       for (String skin : installedSkins) {
/* 158 */         if ((requestedSkin.equals(skin)) && (
/* 159 */           (allowedSkins.size() == 0) || (allowedSkins.contains(skin)))) {
/* 160 */           return true;
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 165 */     return false;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/util/SkinUtil.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */